package com.ylkj.timer.task;


import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.MDC;

import com.ylkj.timer.bo.ORA_SOURCE;
import com.ylkj.timer.bo.ORA_TARGET;
import com.ylkj.timer.service.impl.CommonServiceImpl;

/**
 * 兰州新区oracle数据库-数据抽取任务
 * @author zjf
 *
 */
public class OracleTask {
	private static final Log log = LogFactory.getLog(OracleTask.class);
	private static final String NAMESPACE = "com.ylkj.timer.dao";
	private CommonServiceImpl local_commonService;
	private CommonServiceImpl remote_commonService;
	public CommonServiceImpl getLocal_commonService() {
		return local_commonService;
	}
	public void setLocal_commonService(CommonServiceImpl localCommonService) {
		local_commonService = localCommonService;
	}
	public CommonServiceImpl getRemote_commonService() {
		return remote_commonService;
	}
	public void setRemote_commonService(CommonServiceImpl remoteCommonService) {
		remote_commonService = remoteCommonService;
	}
	/**
	 * 每5分钟从源数据库中获取最新数据，可配置
	 */
	public  void work1() {
		
		MDC.put("DBTYPE", "1");
		ORA_SOURCE ora_source = new ORA_SOURCE();
		ora_source.setNum(1);
		List<ORA_TARGET> list = null;
		ORA_TARGET ora_target = new ORA_TARGET();
		try {
			list = remote_commonService.getCommonDao().find(NAMESPACE, "queryForREMOTE", ora_source);
			if(list!=null&&list.size()!=0)
			{
				
				ora_target = (ORA_TARGET) list.get(0);
				ora_target.setMark("1");
				log.debug("定时抽取："+ora_target.toString());
				local_commonService.insert(NAMESPACE, "insertForLOCAL", ora_target);
			}
		} catch (Exception e) {
			log.debug(e.getMessage());
			//捕获异常，确保每5分钟一条数据
			try {
				ora_target.setNum(1);
				list = local_commonService.getCommonDao().find(NAMESPACE, "queryForLOCAL", ora_target);
				ora_target = list.get(0);
				ora_target.setMark("0");
				ora_target.setInsert_date(new Date());
				local_commonService.insert(NAMESPACE, "insertForLOCAL", ora_target);
			} catch (Exception e1) {
				log.debug("插入本地数据库出错！");
			}
		}
	}
	/**
	 * 每1小时从源数据库中获取最新数据，可配置
	 */
	public void work2() {
		MDC.put("DBTYPE", "1");
		int count ;
		HashMap hm = new HashMap();
		try {
			//获取到目前需要填补的条数
			List list = local_commonService.getCommonDao().find(NAMESPACE, "queryForCount", null);
			hm = (HashMap) list.get(0);
			count = Integer.parseInt(hm.get("COUNT").toString());
			//根据需要填补的条数去源数据库获取对应数据
			ORA_SOURCE ora_source = new ORA_SOURCE();
			ora_source.setNum(count);
			List<ORA_TARGET> ORA_TARGETlist = null;
			ORA_TARGET ora_target = new ORA_TARGET();
	
			ORA_TARGETlist = remote_commonService.getCommonDao().find(NAMESPACE, "queryForREMOTE", ora_source);
			//删除本地需要填补的数据，循环插入获取最新的填补数据
			if(ORA_TARGETlist!=null&&ORA_TARGETlist.size()!=0)
			{
				//删除并验证
				int deletecount = local_commonService.delete(NAMESPACE, "deleteForLOCAL",null);
				if(deletecount==count)
				{
					//填补数据循环插入
					for(int i=0;i<ORA_TARGETlist.size();i++)
					{
						ora_target =  ORA_TARGETlist.get(i);
						ora_target.setMark("1");
						log.debug("定时填补："+ora_target.toString());
						local_commonService.insert(NAMESPACE, "insertForLOCAL", ora_target);
					}
				}
				else{
					log.debug("删除数量不一致"+"删除数量："+deletecount+" 需填补数量"+count);
				}
			}
			else
			{
				log.debug("无数据需要填补");
			}
		} catch (Exception e) {
			log.debug("queryForCount 执行失败");
		}
	}
}
